/******************************************************************************
 * Author:      671643387
 * Created:     2016/10/11
 *****************************************************************************/
var mjMark = require('../mjmark.js');

// -- 捉老麻子扑克
function SubGame(owner) {
    this.owner                = owner;  // 游戏房间
}

SubGame.prototype = {

    /**
     * 初始化数据
     * @param firstRound    是不是第一轮
     */
    onInit: function(firstRound) {
    },

    /**
     * 有玩家胡牌后游戏能否继续
     * @returns {boolean}
     */
    canGameContinueWhenPlayerHuCard: function() {
        return false;
    },

    /**
     * 有人加入房间
     * @param playerIndex
     * @param player
     */
    onPlayerJoinedRoom: function(playerIndex, player) {

    },

    /**
     * 玩家出牌
     * @param player
     * @param card
     */
    onPlayerPlayCard: function(player, card) {

    },

    SettlementsBomb: function ( bombPlayer, room) {
        room.enumerationPlayer(function ( ePlayerIdx, ePlayer ) {
           if(ePlayer.uid == bombPlayer.uid){
               ePlayer.incIntegral(10 * (room.joinedPlayers - 1));
               ePlayer.setBombIntegral(10 * (room.joinedPlayers - 1));
               ePlayer.setAddTotalBomb();
           }else{
               ePlayer.decIntegral(10);
               ePlayer.setCoverBombIntegral(10);
               ePlayer.setAddTotalCoverBomb();
           }
        });
    },

    onSettlementCurrentGame: function() {
        var room = this.owner;
        var _me = this;
        var winPlayerIdx = 0;
        var winScore = 0;
        var winCardNum = 0;
        var isDischargeWater = false;
        var isNoCard = false;
        // 结算每人应该输多少分先记录着， 炸弹直接扣，
        if(room.roundInfo.isNoCard) {
            room.enumerationPlayer( function ( ePlayerIdx, ePlayer ) {
                var numCard = ePlayer.handsPokers.getRemainCardNum();
                if ( ePlayer.isDischargeWater ) {
                    isDischargeWater = true;
                }
                if ( numCard == 0 ) {
                    //手牌等于零，玩家出完牌，
                    winPlayerIdx = ePlayerIdx;
                    ePlayer.setTempScore( 0 );
                    isNoCard = true;
                } else if ( numCard == 1 && ePlayer.handsPokers.hasSameCard( 2 ) ) {
                    // 玩家报一张并且手里是张2，  结算时2没有打出去，扣十分
                    winScore += 10;
                    winCardNum += 1;
                    ePlayer.setTempScore( 10 );
                    ePlayer.setPushMarks( mjMark.PLAY_MARK_OFF_TWO );
                } else if ( numCard == 16 ) {
                    //结算时玩家一张牌都没有出，积分翻倍，即输32积分，2另外再加10分
                    var tmpScore = 0;
                    if ( ePlayer.handsPokers.hasSameCard( 2 ) ) {
                        tmpScore = (numCard - 1) * 2 + 10;
                        winScore += tmpScore;
                        ePlayer.setIsOffTwo( true );
                    } else {
                        tmpScore = numCard * 2;
                        winScore += tmpScore;
                    }
                    winCardNum += 16;
                    ePlayer.setTempScore( tmpScore );
                } else if ( numCard > 1 ) {
                    // 没有报单，剩多少张扣多少分
                    if ( ePlayer.handsPokers.hasSameCard( 2 ) ) {
                        // 结算时有2再手中
                        // 剩余牌数减去一张2， 再加上2没有打出去扣10分
                        var tmpScore = (numCard - 1) + 10;
                        winScore += tmpScore;
                        ePlayer.setTempScore( tmpScore );
                        ePlayer.setIsOffTwo( true );
                    } else {
                        // 玩家手中剩多少牌扣多少分
                        winScore += numCard;
                        ePlayer.setTempScore( numCard );
                    }
                    winCardNum += numCard;
                } else {
                    ePlayer.setTempScore( 0 );
                }

                // 结算炸弹的积分, 作弊时炸弹分不管他们的
                var marks = ePlayer.getMarks();
                if ( marks ) {
                    marks.forEach( function ( mark ) {
                        if ( mark == mjMark.PLAY_CARD_TYPE_BOMB ) {
                            _me.SettlementsBomb( ePlayer, room );
                        }
                    } );
                }
            } );

            if ( isNoCard ) {
                var isHandle = false;
                room.enumerationPlayer( function ( ePlayerIdx, ePlayer ) {
                    ePlayer.setAddTotalSurplusCardNum( ePlayer.handsPokers.getRemainCardNum() );

                    // 赢的玩家跳过
                    if ( ePlayer.handsPokers.getRemainCardNum() == 0 ) {
                        // 赢家一共赢的牌数量
                        ePlayer.handsPokers.num = winCardNum;
                        return;
                    }
                    // 玩家作弊
                    if ( isDischargeWater && ePlayer.isDischargeWater ) {
                        ePlayer.decIntegral( winScore );
                        // 计算作弊的应该付的积分
                        var temp = winScore - ePlayer.tempScore;
                        ePlayer.setAddTmepdischargeWaterNum( temp );
                        ePlayer.setAddTotalCoverCatchPock();
                        isHandle = true;
                        return;
                    }
                    // 已经处理了作弊
                    if ( isHandle ) {
                        return;
                    }
                    // 当前局有人作弊，自己没有作弊，别人包我的积分
                    if ( (ePlayer.isDischargeWater == false && isDischargeWater) ) {
                        return;
                    }
                    //　没有作弊，　手上有牌的玩家扣积分
                    ePlayer.decIntegral( ePlayer.tempScore );
                    ePlayer.setAddTotalCoverCatchPock();
                } );
                var winPlayer = room.players[ winPlayerIdx ];
                //给赢的玩家加积分
                if ( winPlayer ) {
                    winPlayer.incIntegral( winScore );
                    winPlayer.setAddTotalCatchPock();
                }
            }
            winCardNum = 0;
        }
        // 发送结算结果
        var settlementRes = {
            isLastRound: (room.game.totalRound == room.roundInfo.round),
            players: {},
        };
        room.enumerationPlayer(function(ePlayerIdx, ePlayer){
            settlementRes.players[ePlayerIdx] = ePlayer.getSettlementInfo(room.creator);
        });

        if(!settlementRes){
            settlementRes = null;
        }
        room.boradcastMessage('setSettlement', settlementRes);
    },

};

exports.SubGame = SubGame;